<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>TodoList</title>
	<script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
	<div id="app">
		<form v-on:submit.prevent="addNewTodo">
			<input 
			    v-model="newTodoText"
			    id="new-todo"
			    placeholder="Enter a word"
			>
			<button>Add</button>
			<span>{{newTodoText}}</span>
			<input type="button" value="提交" v-on:click="addNewTodo">
		</form>
		<ul>
			<li
			  is="todo-item"
			  v-for="(todo, index) of todos"
			  v-bind:keys="todo.id"
			  v-bind:title="todo.title"
			  v-on:remove="todos.splice(index,1)"
			></li>
		</ul>
	</div>
	<script type="text/javascript">
		Vue.component('todo-item', {
			props: ['title','keys'],
			template:'\
			<li>\
				{{keys}}--{{title}}\
				<button v-on:click="$emit(\'remove\')">Remove</button>\
			</li>\
			'
		})

		var vm = new Vue({
			el: '#app',
			data: {
				newTodoText:'',
				todos: [
					{
						id: 1,
						title: 'title1'
					},
					{
						id: 2,
						title: 'title2'
					},
					{
						id: 3,
						title: 'title3'
					}
				],
				nextTodoId: 4
			},
			methods: {
				addNewTodo: function () {
					this.todos.push({
						id: this.nextTodoId++,
						title: this.newTodoText
					})
					this.newTodoText = ''
				}
			}
		})
	</script>
</body>
</html>